import OSS from 'ali-oss';
import { Controller } from 'egg';
import { v4 as uuidv4 } from 'uuid';


export default class ApiController extends Controller {
    constructor(ctx) {
        super(ctx);
        const { oss } = this.ctx.app.config;
        this.ctx.client = new OSS(oss);
    }

    async upload() {
        const stream = await this.ctx.getFileStream();
        const exts = stream.filename.split('.')
        const ext = exts[exts.length - 1]
        const filename = `public/${uuidv4()}.${ext}`;
        // 创建可写流
        const { url } = await this.ctx.client.put(filename, stream);
        this.ctx.success({
            data: {
                url,
                filename: stream.filename
            },
        });
    }



}
